package dao.entities.control;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.constraints.Pattern;

import org.hibernate.envers.Audited;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import dao.interfaces.IBase;
import utils.SessionUtils;
import views.annotations.View;

@Entity
@Audited
@Table(schema = "GENERAL", name = "CENTRO")
@View.Views({ @View(name = "table", fields = ""),
		@View(name = "record", fields = ""),
		@View(name = "selector", fields = "id,description") })
public class Centro implements IBase {
	@Column(unique = true)
	@Length(max = 30)
	@NotEmpty
	private String description = "";
	@Column(unique = true)
	@NotEmpty
	@Pattern(regexp = "(\\d{8})([-]?)([A-Z]{1})", message = "tipo 12345678-A")
	private String nif;
	@NotEmpty
	@Length(max = 30)
	private String direccion;
	@NotEmpty
	@Length(max = 30)
	private String poblacion;
	@NotEmpty
	@Length(max = 30)
	private String provincia;
	@Column(unique = true)
	@Pattern(regexp = "[9|6|7][0-9]{8}", message = "tipo 960000000")
	@NotEmpty
	private String telefono;
	@Length(max = 30)
	private String contacto;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;
	private String notes;
	private String user;
	private Date date;

	@PreUpdate
	@PrePersist
	private void updateSave() {
		date = new Date();
		user = (String) SessionUtils.getSessionAttribute("user");
	}

	@Override
	public Object getId() {
		return id;
	}

	@Override
	public void setId(Object id) {
		this.id = (long) id;
	};

	@Override
	public String getDescription() {
		return description;
	};

	@Override
	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String getNotes() {
		return notes;
	};

	@Override
	public void setNotes(String notes) {
		this.notes = notes;
	}

	@Override
	public String getUser() {
		return user;
	};

	@Override
	public void setUser(String user) {
		this.user = user;
	}

	@Override
	public Date getDate() {
		return date;
	}

	@Override
	public void setDate(Date date) {
		this.date = date;
	}

	public Centro() {
		super();
	}

	public Centro(String description, String notes, String user, Date date) {
		super();
		this.description = description;
		this.notes = notes;
		this.user = user;
		this.date = date;
	}

	@Override
	public String toString() {
		return description;
	}

	public String getNif() {
		return nif;
	}

	public void setNif(String nif) {
		this.nif = nif;
	}

	public String getDireccion() {
		return direccion;
	}

	public void setDireccion(String direccion) {
		this.direccion = direccion;
	}

	public String getPoblacion() {
		return poblacion;
	}

	public void setPoblacion(String poblacion) {
		this.poblacion = poblacion;
	}

	public String getProvincia() {
		return provincia;
	}

	public void setProvincia(String provincia) {
		this.provincia = provincia;
	}

	public String getTelefono() {
		return telefono;
	}

	public void setTelefono(String telefono) {
		this.telefono = telefono;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getContacto() {
		return contacto;
	}

	public void setContacto(String contacto) {
		this.contacto = contacto;
	}




}
